package com.xw.controller.manage;

import com.google.common.collect.Lists;
import com.xw.common.Const;
import com.xw.model.DicGroup;
import com.xw.model.DicGroupLimit;
import com.xw.model.User;
import com.xw.service.sysconfig.DicGroupLimitService;
import com.xw.service.sysconfig.DicGroupService;
import com.xw.service.sysconfig.UserService;
import com.xw.util.AppUtil;
import com.xw.util.JsonData;
import com.xw.util.PropertiesUtil;
import com.xw.vo.UserVo;
import com.xw.vo.layui.LayUiPhotoDetail;
import com.xw.vo.layui.LayUiPhotos;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 管理员 >> 用户表
 * @author wangxiaoxiao
 */
@Controller
@RequestMapping("/manage/user/")
@Scope("prototype")
public class UserController {
    private Map<String,Object> map = null;
    @Resource
    private UserService userService;
    @Resource
    private DicGroupLimitService groupLimitService;
    @Resource
    private DicGroupService groupService;


    @RequestMapping(value = "getJsonPhoto",method = RequestMethod.GET)
    @ResponseBody
    public Object getJsonPhoto(HttpServletRequest request){
        LayUiPhotos layUiPhotos = new LayUiPhotos();
        layUiPhotos.setId("");
        layUiPhotos.setStart(0);
        layUiPhotos.setTitle("");
        List<LayUiPhotoDetail> list = Lists.newArrayList();
        LayUiPhotoDetail layUiPhotoDetail = new LayUiPhotoDetail();
        layUiPhotoDetail.setAlt("sea");
        layUiPhotoDetail.setPid("");
        layUiPhotoDetail.setSrc("http://develop-kingsuper.oss-cn-hangzhou.aliyuncs.com/02e6289015974781a2f693977abf4ea9.jpg");
        layUiPhotoDetail.setThumb("");
        list.add(layUiPhotoDetail);

        LayUiPhotoDetail layUiPhotoDetail2 = new LayUiPhotoDetail();
        layUiPhotoDetail2.setAlt("sea");
        layUiPhotoDetail2.setPid("");
        layUiPhotoDetail2.setSrc("http://develop-kingsuper.oss-cn-hangzhou.aliyuncs.com/1544786705973");
        layUiPhotoDetail2.setThumb("");
        list.add(layUiPhotoDetail2);
        layUiPhotos.setData(list);
        return layUiPhotos;
    }



    /**
     * 跳转到后台管理员列表页面
     * @return
     */
    @RequestMapping(value = "findAllAdmin",method = RequestMethod.GET)
    public String findAllAdmin(HttpServletRequest request, String glId){
        DicGroupLimit model = groupLimitService.queryByUUID(glId);
        request.setAttribute("model",model);
        request.setAttribute(Const.URL, PropertiesUtil.getProperty("URL"));
        return "adminuser/list";
    }

    /**
     * 获取列表数据
     * @return
     */
    @RequestMapping(value = "findAllAdminData",method = RequestMethod.GET)
    @ResponseBody
    public Object findAllAdminData(HttpSession session, UserVo user){
        Integer count=userService.findByWhere(user).size();
        //1.用户 2.系统用户
        user.setUserType(2);
        List<UserVo> list=userService.findByWhere(user);
        return JsonData.returnLayuiMap(list, count);
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @RequestMapping(value = "goAdd",method = RequestMethod.GET)
    public String goAdd(HttpServletRequest request){
        List<DicGroup> groupList = groupService.queryAll();
        request.setAttribute("groupList",groupList);
        return "adminuser/add";
    }

    /**
     * 添加系统管理员
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public Object add(User user){
        return userService.add(user);
    }

    /**
     * 跳转到编辑页面
     * @return
     */
    @RequestMapping(value = "goEdit",method = RequestMethod.GET)
    public String goEdit(HttpServletRequest request,String userId){
        User user = userService.queryByUUID(userId);
        List<DicGroup> groupList = groupService.queryAll();
        request.setAttribute("groupList",groupList);
        request.setAttribute("model",user);
        request.setAttribute(Const.URL, PropertiesUtil.getProperty("URL"));
        return "adminuser/edit";
    }

    /**
     * 编辑修改
     * @param request
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public Object update(HttpServletRequest request,User user){
        //如果传值为空，则默认给个null
//        if ("".equals(user.getUserPwd())){
//            user.setUserPwd(null);
//        }

        /*
            判断登录名是否和原来的一致
            不一致就要查询是否存在相同的登录名
        */
        User model = userService.queryByUUID(user.getUserId());
        Integer integer;
        if (model.getUserLoginName().equals(user.getUserLoginName())){
            if (StringUtils.isNotBlank(user.getUserPwd())){
                user.setUserPwd(AppUtil.md5Salt(user.getUserPwd()));
            }else{
                user.setUserPwd(null);
            }
            integer= userService.updateSelective(user);
        }else{
            model = new User();
            model.setUserLoginName(user.getUserLoginName());
            model.setUserType(2);
            Integer num = userService.queryCount(model);
            if (num>0){
                return JsonData.fail("账号已存在");
            }else{
                if (!"".equals(user.getUserPwd())){
                    user.setUserPwd(AppUtil.md5Salt(user.getUserPwd()));
                }
                integer= userService.updateSelective(user);
            }
        }
        return JsonData.returnMap(integer);
    }


    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(String id){
        Integer integer = userService.deleteByUUId(id);
        return JsonData.returnMap(integer);
    }
    /**
     * 批量删除
     * @return
     */
    @RequestMapping(value = "dels",method = RequestMethod.POST)
    @ResponseBody
    public Object dels(String... userIds){
        Map map=userService.dels(userIds);
        return map;
    }

    /**
     * 跳转到登录页面
     * @param request
     * @return
     */
    @RequestMapping("tologin")
    public String tologin(HttpServletRequest request){
        return "/login.jsp";
    }

    /**
     * 登录方法
     * @param request
     * @param user
     * @return
     */
    @RequestMapping("login")
    public String login(HttpServletRequest request, User user){
        HttpSession session = request.getSession();
        User model = new User();
        String userLoginName = request.getParameter("userLoginName");
        //1.用户2.系统用户
        model.setUserType(2);
        model.setUserLoginName(userLoginName);
        User user1 = userService.queryOne(model);
        if (user1==null){
            request.setAttribute("msg","该用户不存在");
            //转发的形式
            return "login";
        }

        if (!AppUtil.md5Salt(user.getUserPwd()).equals(user1.getUserPwd())){
            request.setAttribute("msg","密码有误");
            return "login";
        }
        List<DicGroupLimit> groupLimitList=groupLimitService.findGroupLimitByGroupId(user1.getGroupId());
        //查询登录人的基础信息
        UserVo userVo = new UserVo();
        userVo.setUserId(user1.getUserId());
        UserVo userVo1 = userService.findByWhere(userVo).get(0);
        System.out.println(userVo1);

        session.setAttribute("user",user1);
        request.setAttribute("model",userVo1);
        request.setAttribute("groupLimitList",groupLimitList);
        request.setAttribute("code", Const.SUCCESS_CODE);
        request.setAttribute(Const.URL,PropertiesUtil.getProperty("URL"));

        return "admin";
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping(value = "exit",method = RequestMethod.GET)
    public String exit(HttpServletRequest request){
        /*退出将session清空*/
        request.getSession().removeAttribute("user");
        return "redirect:/login.jsp";
    }

}
